package com.diamond.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;



public class Ehcache extends HttpServlet {
	private static Cache cache = null;
	public void init(ServletConfig config) throws ServletException {
		CacheManager manager = new CacheManager(config.getServletContext()
				.getRealPath("/")
				+ "WEB-INF/classes/ehcache.xml");
		cache = manager.getCache("sampleCache1");
		try {
	

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Cache getCache() {
		return cache;
	}
	public static void put(String key,Object value){
		if(key==null||value==null)return;
		if(cache!=null)cache.put(new Element(key, value));
	}
	public static Object get(String key){
		if(key==null)return null;
		if(cache!=null){
			Element element=cache.get(key);
			if(element!=null)
				return element.getValue();
		}
		return null;
	}

}
